package com.example.b_308.myapplication.view.user.register.createdepartment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.model.Department;
import com.example.b_308.myapplication.view.main.Base.BaseLvAdapter;

import java.util.List;

/**
 * Created by B-308 on 2017/12/6.
 */
public class DepartmentAdapter extends BaseLvAdapter {
    List<Department> departments;
    Context context;
    LayoutInflater inflater;

    public DepartmentAdapter(List<Department> departments, Context context) {
        this.departments = departments;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }
    public void addDepartment(Department department){
        departments.add(department);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return departments.size();
    }

    @Override
    public Department getItem(int position) {
        return departments.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DepartmentViewHolder departmentViewHolder;
        if(null == convertView){
            convertView = inflater.inflate(R.layout.item_department,null,false);
            departmentViewHolder = new DepartmentViewHolder(convertView);
            convertView.setTag(departmentViewHolder);
        }else{
            departmentViewHolder = (DepartmentViewHolder)convertView.getTag();
        }
        departmentViewHolder.tv_create_department.setText(getItem(position).getName());
        return convertView;
    }

    private class DepartmentViewHolder {
        TextView tv_create_department;

        public DepartmentViewHolder(View converView) {
            tv_create_department = (TextView) converView.findViewById(R.id.tv_create_department);
        }
    }
}
